package org.tokaf.bestcolumnfinder;

import org.tokaf.TopKElement;
import org.tokaf.algorithm.Algorithm;
import org.tokaf.datasearcher.DataSearcher;
import org.tokaf.rater.Rater;

/**
 * <p>The weight of column is proportional to derivation in aggregating function, multiplied by actual value of current element in column. 
 *  </p>
 * <p>Copyright (c) 2006</p>
 * @author Alan Eckhardt
 * @version 1.0
 */
public class DerivationInPointDataDerivationFinder extends
		DerivationInPointFinder {
	public DerivationInPointDataDerivationFinder(DataSearcher[] data,
			Rater rater) {
		super(data, rater);
	}

	public double[] getDerivations() {
		return derivations;
	}

	protected boolean computeDerivationsInThreshold(Algorithm alg) {
		boolean end = true;
		Rater rater = alg.getRater();
		DataSearcher[] data = alg.getDataSearchers();
		TopKElement threshold = alg.getThreshold();
		//if (nextValue(data) == -1)
		for (int i = 0; i < myData.length; i++) {
			if (!myData[i].hasNext())
				continue;

			if (threshold == null) {
				throw new NullPointerException();
			}
			derivations[i] = rater.getDerivation(i, data, threshold);
			if (!threshold.isNull(i))
				derivations[i] *= threshold.getRating(i);
			if (derivations[i] != 0)
				end = false;
		}
		return end;
	}

	protected boolean computeDerivationsInData(Algorithm alg) {
		boolean end = true;
		Rater rater = alg.getRater();
		DataSearcher[] data = alg.getDataSearchers();
		//if (nextValue(data) == -1)
		for (int i = 0; i < myData.length; i++) {
			if (!myData[i].hasNext())
				continue;

			TopKElement el = alg.findEntity(last[i]);
			if (el == null) {
				throw new NullPointerException();
			}
			//Object[] values = el.getRatings();
			//actualDerivations[i] = rater.getDerivation(i, data, values);
			//if (actualDerivations[i] != 0)

			// dF/dxi
			derivations[i] = rater.getDerivation(i, data, el);
			if (!el.isNull(i))
				derivations[i] *= el.getRating(i);
			if (derivations[i] != 0)
				end = false;
		}
		return end;
	}

}
